
package com.hlkj.pay.app.merchant.ext.impl.yeepay;
/*
 * Hlpay-Plus aggregate payment system.
 * Copyright (c) 2024-2025 Hlpay Team Copyright has the right of final interpretation.
 */

import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Repository;

import com.hlkj.framework.common.pojo.CommonResult;
import com.hlkj.framework.common.util.json.JsonUtils;
import com.hlkj.framework.extension.annotation.Extension;
import com.hlkj.pay.app.merchant.ext.ChannelMerchantProfitOrderExtPt;
import com.hlkj.pay.app.merchant.ext.MerchantExtConstant;
import com.hlkj.pay.app.merchant.ext.dto.profit.ChannelProfitOrderContext;
import com.hlkj.pay.app.merchant.ext.dto.profit.ChannelProfitOrderQueryContext;
import com.hlkj.pay.app.merchant.ext.impl.yeepay.constant.YeePayRsaUtil;
import com.hlkj.pay.app.merchant.ext.impl.yeepay.dto.*;
import com.hlkj.pay.app.merchant.ext.impl.yeepay.handler.YeePayCommonParamsHandle;
import com.hlkj.pay.common.MerchantResultCode;
import com.hlkj.pay.dto.order.PayOrderFundDto;
import com.hlkj.pay.dto.order.RefundOrderFundDto;
import com.hlkj.pay.enums.PayOrderEnums;
import com.hlkj.pay.infrastructure.model.merchant.MerchantAppChannelDO;
import com.hlkj.pay.infrastructure.model.order.OrderFundDO;
import com.hlkj.pay.infrastructure.model.order.OrderFundDetailDO;
import com.hlkj.pay.infrastructure.model.order.PayOrderDO;
import com.hlkj.pay.util.DateUtils;
import com.yeepay.yop.sdk.auth.credentials.PKICredentialsItem;
import com.yeepay.yop.sdk.auth.credentials.YopPKICredentials;
import com.yeepay.yop.sdk.security.CertTypeEnum;
import com.yeepay.yop.sdk.service.divide.DivideClient;
import com.yeepay.yop.sdk.service.divide.DivideClientBuilder;
import com.yeepay.yop.sdk.service.divide.model.*;
import com.yeepay.yop.sdk.service.divide.request.*;
import com.yeepay.yop.sdk.service.divide.response.*;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * @author HlpayTeam
 * @date 2025/01/06 17:47
 */
@Slf4j
@Repository
@RequiredArgsConstructor
@Extension(bizId = MerchantExtConstant.CHANNEL_YEE_PAY)
public class YeePayMerchantProfitOrderExtPt implements ChannelMerchantProfitOrderExtPt {

    private final YeePayCommonParamsHandle yeePayCommonParamsHandle;

    private static final DivideClient api = DivideClientBuilder.builder().build();

    @Override
    public void submitOrderProfit(ChannelProfitOrderContext channelProfitOrderContext) {
        // 配置转换
        CommonResult<YeePayConfig> yeePayConfigCommonResult = yeePayCommonParamsHandle.yeePayConfig(channelProfitOrderContext.getProviderChannelConfig(),
                channelProfitOrderContext.isTest());
        if (!yeePayConfigCommonResult.isSuccess()) {
            channelProfitOrderContext.setRemark(yeePayConfigCommonResult.getMsg());
            channelProfitOrderContext.setProcessStatus(false);
            return;
        }
        // 易宝配置信息
        YeePayConfig yeePayConfig = yeePayConfigCommonResult.getData();
        // 支付订单信息
        PayOrderDO payOrderDO = channelProfitOrderContext.getPayOrderDO();
        // 商户通道信息
        MerchantAppChannelDO merchantAppChannelDO = channelProfitOrderContext.getMerchantAppChannelDO();
        try {
            YeePayAppChannelParams yeeAppChannelParams = JsonUtils.parseObject(merchantAppChannelDO.getAppConfigJson(), YeePayAppChannelParams.class);
            List<PayOrderFundDto> payOrderFundList = channelProfitOrderContext.getPayOrderFundDtoList();
            // 批次号
            String batchNo = payOrderFundList.get(0).getBatchNo();

            ApplyRequest request = new ApplyRequest();
            String appKey = yeePayConfig.getAppId();
            request.getRequestConfig().setAppKey(appKey);
            request.getRequestConfig().setCredentials(new YopPKICredentials(
                    appKey, new PKICredentialsItem(YeePayCommonParamsHandle.getPrivateKey(
                    yeePayConfig.getPrivateKey(),
                    CertTypeEnum.RSA2048),
                    CertTypeEnum.RSA2048)));

            // 上级商户编号
            if(StrUtil.isEmpty(yeePayConfig.getPlatformNo())){
                request.setParentMerchantNo(yeePayConfig.getMchId());
            }else{
                request.setParentMerchantNo(yeePayConfig.getPlatformNo());
            }
            request.setMerchantNo(yeeAppChannelParams.getSubMchId());
            request.setOrderId(payOrderDO.getPayOrderNo());
            request.setDivideRequestId(batchNo);

            // 组装分账详情
            List<YeePayDivideDetail> divideDetails = new ArrayList<>();
            for(PayOrderFundDto fundDto:payOrderFundList){
                YeePayDivideDetail divideDetail = new YeePayDivideDetail();
                divideDetail.setAmount(NumberUtil.decimalFormat("0.00", NumberUtil.parseDouble(fundDto.getAmount()), RoundingMode.HALF_UP));
                divideDetail.setLedgerNo(fundDto.getAccountNo());
                divideDetail.setDivideDetailDesc(fundDto.getDescription());
                divideDetails.add(divideDetail);
            }
            request.setDivideDetail(JsonUtils.toJsonString(divideDetails));

            log.info("易宝提交分账 请求参数:{}", JSONUtil.toJsonStr(request));
            ApplyResponse response = api.apply(request);
            YopOrderDivideResDTO result = response.getResult();
            log.info("易宝提交分账 返回参数:{}", JSONUtil.toJsonStr(result));

            if("OPR00000".equals(result.getCode())) {
                PayOrderEnums.PAY_ORDER_FUND_STATUS payOrderFundStatus = null;
                String failMsg = null;
                switch (result.getStatus()){
                    case "PROCESSING":
                        payOrderFundStatus = PayOrderEnums.PAY_ORDER_FUND_STATUS.FUNDING;
                        break;
                    case "SUCCESS":
                        payOrderFundStatus = PayOrderEnums.PAY_ORDER_FUND_STATUS.SUCCESS;
                        break;
                    case "FAIL":
                        payOrderFundStatus = PayOrderEnums.PAY_ORDER_FUND_STATUS.FAIL;
                        failMsg = result.getMessage();
                        break;
                }

                for(PayOrderFundDto payOrderFundDO:payOrderFundList){
                    payOrderFundDO.setFundStatus(payOrderFundStatus);
                    payOrderFundDO.setType(yeePayCommonParamsHandle.getDivideDetailNo(payOrderFundDO.getAccountNo(),result.getDivideDetail(),true));
                    payOrderFundDO.setDetailId(yeePayCommonParamsHandle.getDivideDetailNo(payOrderFundDO.getAccountNo(),result.getDivideDetail(),false));
                    payOrderFundDO.setSuccessTime(DateUtils.shanghaiDate(result.getDivideSuccessDate(), DatePattern.NORM_DATETIME_PATTERN));
                    if(StrUtil.isNotEmpty(failMsg)){
                        payOrderFundDO.setFailReason(failMsg);
                    }
                }
            }else{
                channelProfitOrderContext.setRemark("易宝提交分账请求失败,失败信息: " +result.getMessage());
                channelProfitOrderContext.setProcessStatus(Boolean.FALSE);
            }

        }
        catch (Exception e) {
            e.printStackTrace();
            channelProfitOrderContext.setRemark("易宝提交订单分账请求失败,失败信息:" + e.getMessage());
            channelProfitOrderContext.setProcessStatus(Boolean.FALSE);
        }
    }

    @Override
    public void queryOrderProfit(ChannelProfitOrderQueryContext channelProfitOrderQueryContext) {
        // 配置转换
        CommonResult<YeePayConfig> yeePayConfigCommonResult = yeePayCommonParamsHandle.yeePayConfig(channelProfitOrderQueryContext.getProviderChannelConfig(),
                channelProfitOrderQueryContext.isTest());
        if (!yeePayConfigCommonResult.isSuccess()) {
            channelProfitOrderQueryContext.setRemark(yeePayConfigCommonResult.getMsg());
            channelProfitOrderQueryContext.setProcessStatus(false);
            return;
        }
        // 易宝配置信息
        YeePayConfig yeePayConfig = yeePayConfigCommonResult.getData();
        // 支付订单信息
        PayOrderDO payOrderDO = channelProfitOrderQueryContext.getPayOrderDO();
        // 商户通道信息
        MerchantAppChannelDO merchantAppChannelDO = channelProfitOrderQueryContext.getMerchantAppChannelDO();
        try {
            YeePayAppChannelParams yeeAppChannelParams = JsonUtils.parseObject(merchantAppChannelDO.getAppConfigJson(), YeePayAppChannelParams.class);
            List<OrderFundDetailDO> payOrderFundList = channelProfitOrderQueryContext.getPayOrderFunds();
            // 批次号
            String batchNo = payOrderFundList.get(0).getBatchNo();

            QueryRequest request = new QueryRequest();
            String appKey = yeePayConfig.getAppId();
            request.getRequestConfig().setAppKey(appKey);
            request.getRequestConfig().setCredentials(new YopPKICredentials(
                    appKey, new PKICredentialsItem(YeePayCommonParamsHandle.getPrivateKey(
                    yeePayConfig.getPrivateKey(),
                    CertTypeEnum.RSA2048),
                    CertTypeEnum.RSA2048)));

            // 上级商户编号
            if(StrUtil.isEmpty(yeePayConfig.getPlatformNo())){
                request.setParentMerchantNo(yeePayConfig.getMchId());
            }else{
                request.setParentMerchantNo(yeePayConfig.getPlatformNo());
            }
            request.setMerchantNo(yeeAppChannelParams.getSubMchId());
            request.setOrderId(payOrderDO.getPayOrderNo());
            request.setDivideRequestId(batchNo);

            log.info("易宝提交分账查询 请求参数:{}", JSONUtil.toJsonStr(request));
            QueryResponse response = api.query(request);
            YopQueryOrderDivideResDTO result = response.getResult();
            log.info("易宝提交分账查询 返回参数:{}", JSONUtil.toJsonStr(result));

            if("OPR00000".equals(result.getCode())) {
                PayOrderEnums.PAY_ORDER_FUND_STATUS payOrderFundStatus = null;
                String failMsg = null;
                switch (result.getStatus()){
                    case "PROCESSING":
                        payOrderFundStatus = PayOrderEnums.PAY_ORDER_FUND_STATUS.FUNDING;
                        break;
                    case "SUCCESS":
                        payOrderFundStatus = PayOrderEnums.PAY_ORDER_FUND_STATUS.SUCCESS;
                        break;
                    case "FAIL":
                        payOrderFundStatus = PayOrderEnums.PAY_ORDER_FUND_STATUS.FAIL;
                        failMsg = result.getMessage();
                        break;
                }
                for(OrderFundDetailDO fundDetailDO:payOrderFundList){
                    fundDetailDO.setDetailId(yeePayCommonParamsHandle.getDivideDetailNo(fundDetailDO.getAccountNo(),result.getDivideDetail(),false));
                    fundDetailDO.setType(yeePayCommonParamsHandle.getDivideDetailNo(fundDetailDO.getAccountNo(),result.getDivideDetail(),true));
                    fundDetailDO.setFundStatus(payOrderFundStatus.getCode());
                    fundDetailDO.setSuccessTime(DateUtils.shanghaiDate(result.getDivideSuccessDate(), DatePattern.NORM_DATETIME_PATTERN));
                    if(StrUtil.isNotEmpty(failMsg)){
                        fundDetailDO.setFailReason(failMsg);
                    }
                }
            }else{
                channelProfitOrderQueryContext.setRemark("易宝提交分账查询请求失败,失败信息: " +result.getMessage());
                channelProfitOrderQueryContext.setProcessStatus(Boolean.FALSE);
            }
        }
        catch (Exception e) {
            e.printStackTrace();
            channelProfitOrderQueryContext.setRemark("易宝提交分账查询请求失败,失败信息:" + e.getMessage());
            channelProfitOrderQueryContext.setProcessStatus(Boolean.FALSE);
        }
    }

    @Override
    public void submitRefundOrderProfit(ChannelProfitOrderContext channelProfitOrderContext) {
        // 配置转换
        CommonResult<YeePayConfig> yeePayConfigCommonResult = yeePayCommonParamsHandle.yeePayConfig(channelProfitOrderContext.getProviderChannelConfig(),
                channelProfitOrderContext.isTest());
        if (!yeePayConfigCommonResult.isSuccess()) {
            channelProfitOrderContext.setRemark(yeePayConfigCommonResult.getMsg());
            channelProfitOrderContext.setProcessStatus(false);
            return;
        }
        // 易宝配置信息
        YeePayConfig yeePayConfig = yeePayConfigCommonResult.getData();
        // 支付订单信息
        PayOrderDO payOrderDO = channelProfitOrderContext.getPayOrderDO();
        // 商户通道信息
        MerchantAppChannelDO merchantAppChannelDO = channelProfitOrderContext.getMerchantAppChannelDO();
        try {
            YeePayAppChannelParams yeeAppChannelParams = JsonUtils.parseObject(merchantAppChannelDO.getAppConfigJson(), YeePayAppChannelParams.class);
            List<RefundOrderFundDto> payOrderFundList = channelProfitOrderContext.getRefundOrderFundDtoList();
            // 退款批次号
            String refundBatchNo = payOrderFundList.get(0).getBatchNo();

            BackRequest request = new BackRequest();
            String appKey = yeePayConfig.getAppId();
            request.getRequestConfig().setAppKey(appKey);
            request.getRequestConfig().setCredentials(new YopPKICredentials(
                    appKey, new PKICredentialsItem(YeePayCommonParamsHandle.getPrivateKey(
                    yeePayConfig.getPrivateKey(),
                    CertTypeEnum.RSA2048),
                    CertTypeEnum.RSA2048)));

            // 上级商户编号
            if(StrUtil.isEmpty(yeePayConfig.getPlatformNo())){
                request.setParentMerchantNo(yeePayConfig.getMchId());
            }else{
                request.setParentMerchantNo(yeePayConfig.getPlatformNo());
            }
            request.setMerchantNo(yeeAppChannelParams.getSubMchId());
            request.setOrderId(payOrderDO.getPayOrderNo());
            request.setDivideRequestId(payOrderDO.getPayOrderNo());
            request.setDivideBackRequestId(refundBatchNo);

            // 组装分账退款详情
            List<YeePayDivideBackDetail> backDetails = new ArrayList<>();
            for(RefundOrderFundDto fundDto:payOrderFundList){
                YeePayDivideBackDetail divideBackDetail = new YeePayDivideBackDetail();
                divideBackDetail.setAmount(NumberUtil.decimalFormat("0.00", NumberUtil.parseDouble(fundDto.getAmount()), RoundingMode.HALF_UP));
                divideBackDetail.setDivideDetailNo(fundDto.getDetailId());
                divideBackDetail.setDivideBackReason(fundDto.getDescription());
                backDetails.add(divideBackDetail);
            }
            request.setDivideBackDetail(JSONUtil.toJsonStr(backDetails));

            log.info("易宝提交分账退款 请求参数:{}", JSONUtil.toJsonStr(request));
            BackResponse response = api.back(request);
            YopDivideBackResDTO result = response.getResult();
            log.info("易宝提交分账退款 返回参数:{}", JSONUtil.toJsonStr(result));

            if("OPR00000".equals(result.getCode())) {
                PayOrderEnums.PAY_ORDER_FUND_STATUS payOrderFundStatus = null;
                String failMsg = null;
                switch (result.getStatus()){
                    case "PROCESSING":
                        payOrderFundStatus = PayOrderEnums.PAY_ORDER_FUND_STATUS.FUNDING;
                        break;
                    case "SUCCESS":
                        payOrderFundStatus = PayOrderEnums.PAY_ORDER_FUND_STATUS.REFUND;
                        break;
                    case "FAIL":
                        payOrderFundStatus = PayOrderEnums.PAY_ORDER_FUND_STATUS.FAIL;
                        failMsg = result.getFailReason();
                        break;
                }

                for(RefundOrderFundDto payOrderFundDO:payOrderFundList){
                    payOrderFundDO.setFundStatus(payOrderFundStatus);
                    if(StrUtil.isNotEmpty(failMsg)){
                        payOrderFundDO.setFailReason(failMsg);
                    }
                }
            }else{
                channelProfitOrderContext.setRemark("易宝提交分账退款请求失败,失败信息: " +result.getMessage());
                channelProfitOrderContext.setProcessStatus(Boolean.FALSE);
            }
        }
        catch (Exception e) {
            e.printStackTrace();
            log.error("易宝提交订单分润退款请求失败 message:{}", e.getMessage(), e);
            channelProfitOrderContext.setRemark("易宝提交分账退款请求失败,失败信息:" + e.getMessage());
            channelProfitOrderContext.setProcessStatus(Boolean.FALSE);
        }
    }

    @Override
    public void queryRefundOrderProfit(ChannelProfitOrderQueryContext channelProfitOrderQueryContext) {
        // 配置转换
        CommonResult<YeePayConfig> yeePayConfigCommonResult = yeePayCommonParamsHandle.yeePayConfig(channelProfitOrderQueryContext.getProviderChannelConfig(),
                channelProfitOrderQueryContext.isTest());
        if (!yeePayConfigCommonResult.isSuccess()) {
            channelProfitOrderQueryContext.setRemark(yeePayConfigCommonResult.getMsg());
            channelProfitOrderQueryContext.setProcessStatus(false);
            return;
        }
        // 易宝配置信息
        YeePayConfig yeePayConfig = yeePayConfigCommonResult.getData();
        // 支付订单信息
        PayOrderDO payOrderDO = channelProfitOrderQueryContext.getPayOrderDO();
        // 商户通道信息
        MerchantAppChannelDO merchantAppChannelDO = channelProfitOrderQueryContext.getMerchantAppChannelDO();
        try {
            YeePayAppChannelParams yeeAppChannelParams = JsonUtils.parseObject(merchantAppChannelDO.getAppConfigJson(), YeePayAppChannelParams.class);
            List<OrderFundDetailDO> payOrderFundList = channelProfitOrderQueryContext.getRefundOrderFunds();
            // 批次号
            String batchNo = payOrderFundList.get(0).getBatchNo();

            BackQueryRequest request = new BackQueryRequest();
            String appKey = yeePayConfig.getAppId();
            request.getRequestConfig().setAppKey(appKey);
            request.getRequestConfig().setCredentials(new YopPKICredentials(
                    appKey, new PKICredentialsItem(YeePayCommonParamsHandle.getPrivateKey(
                    yeePayConfig.getPrivateKey(),
                    CertTypeEnum.RSA2048),
                    CertTypeEnum.RSA2048)));

            // 上级商户编号
            if(StrUtil.isEmpty(yeePayConfig.getPlatformNo())){
                request.setParentMerchantNo(yeePayConfig.getMchId());
            }else{
                request.setParentMerchantNo(yeePayConfig.getPlatformNo());
            }
            request.setMerchantNo(yeeAppChannelParams.getSubMchId());
            request.setOrderId(payOrderDO.getPayOrderNo());
            request.setDivideBackRequestId(batchNo);

            log.info("易宝提交分账退款查询 请求参数:{}", JSONUtil.toJsonStr(request));
            BackQueryResponse response = api.backQuery(request);
            YopQueryDivideBackResDTO result = response.getResult();
            log.info("易宝提交分账退款查询 返回参数:{}", JSONUtil.toJsonStr(result));

            if("OPR00000".equals(result.getCode())) {

                PayOrderEnums.PAY_ORDER_FUND_STATUS payOrderFundStatus = null;
                String failMsg = null;
                switch (result.getStatus()){
                    case "PROCESSING":
                        payOrderFundStatus = PayOrderEnums.PAY_ORDER_FUND_STATUS.FUNDING;
                        break;
                    case "SUCCESS":
                        payOrderFundStatus = PayOrderEnums.PAY_ORDER_FUND_STATUS.REFUND;
                        break;
                    case "FAIL":
                        payOrderFundStatus = PayOrderEnums.PAY_ORDER_FUND_STATUS.FAIL;
                        failMsg = result.getErrorMessage();
                        break;
                }

                for(OrderFundDetailDO payOrderFundDO:payOrderFundList){
                    payOrderFundDO.setFundStatus(payOrderFundStatus.getCode());
                    payOrderFundDO.setSuccessTime(DateUtils.shanghaiDate(result.getDivideBackSuccessDate(), DatePattern.NORM_DATETIME_PATTERN));
                    if(StrUtil.isNotEmpty(failMsg)){
                        payOrderFundDO.setFailReason(failMsg);
                    }
                }

            }else{
                channelProfitOrderQueryContext.setRemark("易宝提交分账查询请求失败,失败信息: " +result.getMessage());
                channelProfitOrderQueryContext.setProcessStatus(Boolean.FALSE);
            }
        }
        catch (Exception e) {
            e.printStackTrace();
            channelProfitOrderQueryContext.setRemark("易宝提交订单分账退款查询失败,失败信息:" + e.getMessage());
            channelProfitOrderQueryContext.setProcessStatus(Boolean.FALSE);
        }
    }

    @Override
    public void finishAcctOrder(ChannelProfitOrderContext channelProfitOrderContext) {
        // 配置转换
        CommonResult<YeePayConfig> yeePayConfigCommonResult = yeePayCommonParamsHandle.yeePayConfig(channelProfitOrderContext.getProviderChannelConfig(),
                channelProfitOrderContext.isTest());
        if (!yeePayConfigCommonResult.isSuccess()) {
            channelProfitOrderContext.setRemark(yeePayConfigCommonResult.getMsg());
            channelProfitOrderContext.setProcessStatus(false);
            return;
        }
        // 易宝配置信息
        YeePayConfig yeePayConfig = yeePayConfigCommonResult.getData();
        // 支付订单信息
        PayOrderDO payOrderDO = channelProfitOrderContext.getPayOrderDO();
        // 商户通道信息
        MerchantAppChannelDO merchantAppChannelDO = channelProfitOrderContext.getMerchantAppChannelDO();
        try {
            YeePayAppChannelParams yeeAppChannelParams = JsonUtils.parseObject(merchantAppChannelDO.getAppConfigJson(), YeePayAppChannelParams.class);
            OrderFundDO orderFundDO = channelProfitOrderContext.getOrderFundDO();

            CompleteRequest request = new CompleteRequest();
            String appKey = yeePayConfig.getAppId();
            request.getRequestConfig().setAppKey(appKey);
            request.getRequestConfig().setCredentials(new YopPKICredentials(
                    appKey, new PKICredentialsItem(YeePayCommonParamsHandle.getPrivateKey(
                    yeePayConfig.getPrivateKey(),
                    CertTypeEnum.RSA2048),
                    CertTypeEnum.RSA2048)));

            // 上级商户编号
            if(StrUtil.isEmpty(yeePayConfig.getPlatformNo())){
                request.setParentMerchantNo(yeePayConfig.getMchId());
            }else{
                request.setParentMerchantNo(yeePayConfig.getPlatformNo());
            }
            request.setMerchantNo(yeeAppChannelParams.getSubMchId());
            request.setOrderId(payOrderDO.getPayOrderNo());
            request.setDivideRequestId(orderFundDO.getBatchNo());

            log.info("易宝完结分账 请求参数:{}", JSONUtil.toJsonStr(request));
            CompleteResponse response = api.complete(request);
            YopOrderEndDivideResDTO result = response.getResult();
            log.info("易宝完结分账 返回参数:{}", JSONUtil.toJsonStr(result));

            if("OPR00000".equals(result.getCode())) {
                switch (result.getDivideStatus().getValue()){
                    case "PROCESSING":
                    case "SUCCESS":
                        orderFundDO.setFundStatus(PayOrderEnums.PAY_ORDER_FUND_STATUS.CLOSE.getCode());
                        log.info("易宝完结分账 成功:{}", JSONUtil.toJsonStr(result));
                        break;
                    case "FAIL":
                        channelProfitOrderContext.setProcessStatus(false);
                        channelProfitOrderContext.setRemark("易宝完结分账请求失败,失败信息:" + result.getMessage());
                        break;
                }
            }else{
                channelProfitOrderContext.setProcessStatus(false);
                channelProfitOrderContext.setRemark("易宝完结分账请求失败,失败信息:" + result.getMessage());
            }
        }
        catch (Exception e) {
            e.printStackTrace();
            channelProfitOrderContext.setRemark("易宝完结分账请求失败请求失败,失败信息:" + e.getMessage());
            channelProfitOrderContext.setProcessStatus(Boolean.FALSE);
        }
    }

    @Override
    public CommonResult<String> acctStatusParse(HttpServletRequest request, ChannelProfitOrderContext channelProfitOrderContext) {

        CommonResult<String> acctOrderDOCommonResult = CommonResult.success();
        try {
            String paramsConfig = channelProfitOrderContext.getProviderChannelConfig().getParamsConfig();
            // 易宝配置信息
            YeePayConfig yeePayConfig = JsonUtils.parseObject(paramsConfig, YeePayConfig.class);
            // 获取响应内容
            String result = request.getParameter("response");

            log.info("易宝分账订单通知密文 {}", result);
            String plainText = YeePayRsaUtil.decrypt(result, yeePayConfig,true);
            log.info("易宝分账订单通知明文 plainText:{}",plainText);

            if (StrUtil.isEmpty(plainText)) {
                log.info("易宝回调验签不通过,signature:{}", plainText);
                acctOrderDOCommonResult.setCode(MerchantResultCode.MERCHANT_CHANNEL_CALLBACK_SIGN_ERROR.getCode());
            }

            YeePayDivideNotify respData = JsonUtils.parseObject(plainText, YeePayDivideNotify.class);
            OrderFundDO orderFundDO = channelProfitOrderContext.getOrderFundDO();
            // 分账详情
            List<OrderFundDetailDO> orderFundDetailList = channelProfitOrderContext.getOrderFundDetailDOList();

            PayOrderEnums.PAY_ORDER_FUND_STATUS payOrderFundStatus = null;
            String failMsg = null;
            switch (respData.getDivideStatus()){
                case "SUCCESS":
                    payOrderFundStatus = PayOrderEnums.PAY_ORDER_FUND_STATUS.SUCCESS;
                    break;
                case "FAIL":
                    payOrderFundStatus = PayOrderEnums.PAY_ORDER_FUND_STATUS.FAIL;
                    failMsg = respData.getFailReason();
                    break;
            }

            channelProfitOrderContext.setPayOrderFundStatus(payOrderFundStatus);
            for(OrderFundDetailDO fundDetailDO:orderFundDetailList){
                fundDetailDO.setDetailId(yeePayCommonParamsHandle.getDivideDetailNo(fundDetailDO.getAccountNo(),respData.getDivideDetail(),false));
                fundDetailDO.setFundStatus(payOrderFundStatus.getCode());
                fundDetailDO.setType(yeePayCommonParamsHandle.getDivideDetailNo(fundDetailDO.getAccountNo(),respData.getDivideDetail(),true));
                if(StrUtil.isNotEmpty(failMsg)){
                    fundDetailDO.setFailReason(failMsg);
                }
            }
        }
        catch (Exception e) {
            e.printStackTrace();
            acctOrderDOCommonResult.setCode(MerchantResultCode.MERCHANT_CHANNEL_CALLBACK_SIGN_ERROR.getCode());
        }

        log.info("易宝收到分账订单回调,解析后的分账订单数据为 payOrderDO:{}", JsonUtils.toJsonStringNotNull(acctOrderDOCommonResult.getData()));
        return acctOrderDOCommonResult;
    }

    @Override
    public void settleConfirm(ChannelProfitOrderContext channelProfitOrderContext) {

    }
}
